package commons.jdbc.basic;

import com.alibaba.fastjson.JSON;
import commons.r.R;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * commons.jdbc.basic
 * User: 坚持
 * Date: 2024/12/17 18:57
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
public class ResponseUtil {
    public void writeJsonResponse(HttpServletResponse response, R r,String servletName) throws IOException {
        // 获取输出流
        PrintWriter writer = response.getWriter();

        // 将对象转为 JSON 字符串
        String result = JSON.toJSONString(r);

        // 打印日志（可以根据需要去掉）
        System.out.println(servletName +":"+ result);
        // 写入响应
        writer.write(result);

        // 刷新并关闭 writer
        writer.flush();
        writer.close();
    }
}